package com.jincheng.system.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.jincheng.common.utils.DateUtils;
import com.jincheng.system.domain.SysNotice;
import com.jincheng.system.domain.Tribe;
import com.jincheng.system.domain.UserTribe;
import com.jincheng.system.domain.dto.UserTribeDetailDTO;
import com.jincheng.system.domain.dto.UserTribeScreenDTO;
import com.jincheng.system.mapper.SysNoticeMapper;
import com.jincheng.system.mapper.TribeMapper;
import com.jincheng.system.mapper.UserTribeMapper;
import com.jincheng.system.service.IUserTribeService;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.List;

import static com.jincheng.common.utils.SecurityUtils.getUserId;
import static com.jincheng.common.utils.SecurityUtils.getUsername;

@Service
public class UserTribeServiceImpl implements IUserTribeService {
    private final UserTribeMapper userTribeMapper;
    private final SysNoticeMapper noticeMapper;
    private final TribeMapper tribeMapper;
    private final SysNoticeServiceImpl sysNoticeServiceImpl;

    public UserTribeServiceImpl(UserTribeMapper userTribeMapper, SysNoticeMapper sysNoticeMapper, TribeMapper tribeMapper, SysNoticeServiceImpl sysNoticeServiceImpl) {
        this.userTribeMapper = userTribeMapper;
        this.noticeMapper = sysNoticeMapper;
        this.tribeMapper = tribeMapper;
        this.sysNoticeServiceImpl = sysNoticeServiceImpl;
    }

    /**
     * 新增用户部落
     *
     * @param userTribe 用户部落
     */
    @Override
    public int insertUserTribe(UserTribe userTribe) {
        userTribe.setCreateTime(DateUtils.getNowDate());
        userTribe.setUpdateTime(DateUtils.getNowDate());
        return userTribeMapper.insert(userTribe);
    }

    /**
     * 查询用户部落列表
     *
     * @param userTribe 用户部落
     * @return 用户部落集合
     */
    @Override
    public List<UserTribe> selectUserTribeList(UserTribe userTribe) {
        LambdaQueryWrapper<UserTribe> queryWrapper = new QueryWrapper<UserTribe>().lambda();
        if (userTribe.getUserTribeId() != null && userTribe.getUserTribeId() != 0) {
            queryWrapper.eq(UserTribe::getUserTribeId, userTribe.getUserTribeId());
        }
        if (userTribe.getUserId() != null && userTribe.getUserId() != 0) {
            queryWrapper.eq(UserTribe::getUserId, userTribe.getUserId());
        }
        if (userTribe.getTribeId() != null && userTribe.getTribeId() != 0) {
            queryWrapper.eq(UserTribe::getTribeId, userTribe.getTribeId());
        }
        if (userTribe.getStatus() != null) {
            queryWrapper.eq(UserTribe::getStatus, userTribe.getStatus());
        }
        if (userTribe.getCreateTime() != null) {
            queryWrapper.eq(UserTribe::getCreateTime, userTribe.getCreateTime());
        }
        if (userTribe.getUpdateTime() != null) {
            queryWrapper.eq(UserTribe::getUpdateTime, userTribe.getUpdateTime());
        }
        return userTribeMapper.selectList(queryWrapper);
    }

    /**
     * 根据用户id和部落id查询用户部落
     *
     * @param userTribeId 用户部落id
     * @return 用户部落
     */
    @Override
    public UserTribe selectUserTribeById(Long userTribeId) {
        LambdaQueryWrapper<UserTribe> queryWrapper = new QueryWrapper<UserTribe>().lambda();
        queryWrapper.eq(UserTribe::getUserTribeId, userTribeId);
        return userTribeMapper.selectOne(queryWrapper);
    }

    /**
     * 根据用户id和部落id查询用户部落
     *
     * @param tribeID 用户部落id
     * @return 用户部落
     */
    @Override
    public UserTribe selectUserTribeByUserIdTribeId(Long tribeID) {
        LambdaQueryWrapper<UserTribe> queryWrapper = new QueryWrapper<UserTribe>().lambda();
        queryWrapper.eq(UserTribe::getTribeId, tribeID);
        queryWrapper.eq(UserTribe::getUserId, getUserId());
        return userTribeMapper.selectOne(queryWrapper);
    }

    /**
     * 查询用户部落详情列表
     *
     * @param userTribeScreenDTO 用户部落
     * @return 用户部落详情集合
     */
    @Override
    public List<UserTribeDetailDTO> selectUserTribeDetailList(UserTribeScreenDTO userTribeScreenDTO) {
        return userTribeMapper.selectUserTribeDetailList(userTribeScreenDTO);
    }

    /**
     * 更新用户部落
     *
     * @param userTribe 用户部落
     */
    @Override
    public int updateUserTribe(UserTribe userTribe) {
        userTribe.setUpdateTime(DateUtils.getNowDate());
        return userTribeMapper.updateById(userTribe);
    }

    /**
     * 根据id列表删除用户部落信息
     *
     * @param userTribeIds id列表
     * @return 受影响行
     */
    @Override
    public int deleteUserTribeByIds(Long[] userTribeIds) {
        return userTribeMapper.deleteBatchIds(Arrays.asList(userTribeIds));
    }

    /**
     * 更新用户部落状态
     *
     * @param userTribeDetailDTO 用户部落
     */
    @Override
    public int changeStatus(UserTribeDetailDTO userTribeDetailDTO) {
        LambdaUpdateWrapper<UserTribe> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.eq(UserTribe::getUserTribeId, userTribeDetailDTO.getUserTribeId());
        updateWrapper.set(UserTribe::getStatus, userTribeDetailDTO.getStatus());
        int affectedRows = userTribeMapper.update(updateWrapper);
        if (affectedRows > 0) {
            sysNoticeServiceImpl.sendTribeApplyPassNotice(userTribeDetailDTO);
        }
        return affectedRows;
    }

    /**
     * 构建部落申请结果html
     *
     * @param tribeName 部落名称
     * @param reason    拒绝理由，null表示通过
     * @return html
     */
    private String buildHtml(String tribeName, String reason) {
        return "<h2>部落申请结果通知</h2>"
                + "<p>尊敬的用户:</p>"
                + "<p>您申请加入的部落\"" + tribeName + "\"已于" + DateUtils.getDateTime() + "被" + (reason == null ? "通过" : "拒绝")
                + (reason == null ? "<p>您可以在我的部落中查看部落信息</p>" : "<p>拒绝理由：" + reason + "</p>");
    }
}
